# -*- coding: utf-8 -*-

import json
import hashlib
from base import UserCase, Runner, TestSuite


class Case(UserCase):

    def test_my_action_log_list(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        p = {
            'name': self.randhan(8),
        }
        res = self.c.open('/api/my-corp/update', {'name': p['name']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-action-log/list', {'orderBy': 'action_log.id', 'orderDesc': '1'}, 'GET')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)
        self.assertEqual(res['data']['itemList'][0]['type'], '/api/my-corp/update')
        self.assertEqual(json.loads(res['data']['itemList'][0]['target'])['name'], p['name'])

    def test_my_action_log_list_as_normal(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        p = {
            'name': self.randhan(3),
            'role': 'normal',
            'password': hashlib.md5(self.randchar(6)).hexdigest(),
            'username': self.randchar(8),
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-action-log/list')
        self.assertNotEqual(res['code'], 0)


test_list = [
    'test_my_action_log_list',
    'test_my_action_log_list_as_normal',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



 
